package org.enhance.common.util;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeUtil {

    private TimeUtil() {
        throw new IllegalStateException("Constant class");
    }

    public static Date getOffsetDateByDate(int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, offset);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getOffsetDate(int offset, int offsetType) {
        return getOffsetDate(null, offset, offsetType);
    }

    public static Date getOffsetDate(Date date, int offset, int offsetType) {
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        switch (offsetType) {
            case Calendar.YEAR:
                calendar.add(Calendar.YEAR, offset);
                break;
            case Calendar.MONTH:
                calendar.add(Calendar.MONTH, offset);
                break;
            case Calendar.DATE:
                calendar.add(Calendar.DATE, offset);
                break;
            case Calendar.HOUR_OF_DAY:
                calendar.add(Calendar.HOUR_OF_DAY, offset);
                break;
            case Calendar.MINUTE:
                calendar.add(Calendar.MINUTE, offset);
                break;
            case Calendar.SECOND:
                calendar.add(Calendar.SECOND, offset);
                break;
            case Calendar.MILLISECOND:
                calendar.add(Calendar.MILLISECOND, offset);
                break;
        }
        return calendar.getTime();
    }

    public static boolean isMonthFirstDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH) == 1;
    }

    public static List<String> getBetweenMonthStr(String monthDate) {
        List timeStrList = new ArrayList();

        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(DateFormatUtil.parseYearmonth(monthDate));

        calBegin.add(Calendar.MONTH, -1);
        timeStrList.add(DateFormatUtil.toYearmonth(calBegin.getTime()));
        timeStrList.add(monthDate);
        calBegin.add(Calendar.MONTH, 2);
        timeStrList.add(DateFormatUtil.toYearmonth(calBegin.getTime()));

        return timeStrList;
    }

    public static Date getMonthFirstDay() {
        return getMonthFirstDay(new Date());
    }

    public static Date getMonthFirstDay(Date date) {
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);

        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        cale.set(Calendar.HOUR_OF_DAY, 0);
        cale.set(Calendar.MINUTE, 0);
        cale.set(Calendar.SECOND, 0);
        cale.set(Calendar.MILLISECOND, 0);

        return cale.getTime();
    }

    public static List<Date> getMonthBeginEnd(Date date) {
        List<Date> timeList = new ArrayList();

        Calendar cale = Calendar.getInstance();
        cale.setTime(date);

        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        cale.set(Calendar.HOUR_OF_DAY, 0);
        cale.set(Calendar.MINUTE, 0);
        cale.set(Calendar.SECOND, 0);
        cale.set(Calendar.MILLISECOND, 0);
        timeList.add(cale.getTime());
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        cale.set(Calendar.HOUR_OF_DAY, 23);
        cale.set(Calendar.MINUTE, 59);
        cale.set(Calendar.SECOND, 59);
        cale.set(Calendar.MILLISECOND, 59);
        timeList.add(cale.getTime());

        return timeList;
    }

    public static List<String> getBetweenTimeStr(Date begintTime, Date endTime, boolean isDate) {
        int field = Calendar.DAY_OF_MONTH;
        DateFormat format;
        if (isDate) {
            format = DateFormatUtil.getDateFormat();
        } else {
            format = DateFormatUtil.getYearmonthFormat();
            field = Calendar.MONTH;
        }
        List timeStrList = new ArrayList();
        timeStrList.add(format.format(begintTime));

        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(begintTime);

        while (endTime.after(calBegin.getTime())) {
            calBegin.add(field, 1);
            String dayStr = format.format(calBegin.getTime());
            timeStrList.add(dayStr);
        }

        return timeStrList;
    }

}
